﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace eKanbanWPF.View
{
    /// <summary>
    /// Interaction logic for FrmFeature.xaml
    /// </summary>
    public partial class FrmFeature : Window
    {
        readonly static FrmFeature frm = new FrmFeature();
        static bool result = false;

        public static string FeatureName { get { return frm.txtName.Text; } set { frm.txtName.Text = value; } }
        public static string FeatureDescription { get { return frm.txtDescription.Text; } set { frm.txtDescription.Text = value; } }
        public static Decimal FeaturePriority { get { return frm.valPriority.Value; } set { frm.valPriority.Value = value; } }

        private void ResetControl()
        {
            txtName.Text = "";
            txtDescription.Text = "";
            valPriority.Value = 0;
            gridHistory.DataSource = null;
        }

        private void AssignData(eKanbanWPF.Model.Feature feature)
        {
            if (feature != null)
            {
                txtName.Text = feature.Name;
                txtDescription.Text = feature.Description;
                valPriority.Value = feature.Priority;
                gridHistory.DataSource = feature.History;
            }
            else
            {
                ResetControl();
            }
        }

        public static bool Show(eKanbanWPF.Model.Feature feature)
        {
            result = false;
            frm.AssignData(feature);
            //frm.TaskLookUp.ItemsSource = tasks;
            var r = frm.ShowDialog();
            return result;
        }

        public FrmFeature()
        {
            InitializeComponent();
            App.Current.MainWindow.Closed += (sender, e) => this.Close();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.Visibility == System.Windows.Visibility.Visible)
            {
                this.Hide();
                e.Cancel = true;
            }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (Validate())
            {
                result = true;
                this.DialogResult = true;
            }
        }

        private bool Validate()
        {
            var value = txtName.EditValue;
            if (value == null || (value is string && ((string)value).Trim().Length == 0))
            {
                return false;
            }
            return true;
        }



    }
}
